﻿using System;
using System.Collections.Generic;
using System.Text;
using Framework.Message.SMS.CommonLibrary.Generic;

namespace Framework.Message.SMS.SGIP
{
    public class CommandHeader : BaseCommandHeader<SGIPCommandType, CommandSequence>
    {
        /// <summary>
        /// 信息头长度
        /// </summary>
        public static uint HeaderLength
        {
            get
            {
                return (4 + 4 + 12);
            }
        }

        public CommandHeader(SGIPCommandType commandID, uint bodyLength, CommandSequence sequence)
            : base(commandID, sequence)
        {
            this.MessageLength = bodyLength + HeaderLength;
        }

        public CommandHeader(SGIPCommandType commandID, CommandSequence sequence)
            : base(commandID, sequence)
        {
        }

        public CommandHeader(byte[] bytes)
            : base(bytes)
        {
        }

        public CommandHeader(byte[] bytes, int startIndex)
            : base(bytes, startIndex)
        {
            ByteTo(bytes, startIndex);
        }

        protected override void ByteTo(byte[] bytes, int startIndex)
        {
            int i = startIndex;
            this.MessageLength = ByteIntConvert.Bytes2UInt(bytes, i);
            i += 4;
            this._CommandID = (SGIPCommandType)ByteIntConvert.Bytes2UInt(bytes, i);
            i += 4;
            this.SequenceNumber = new CommandSequence(bytes, i);
        }

        public override byte[] ToBytes()
        {
            //消息长度
            byte[] headerBytes = new byte[CommandHeader.HeaderLength];
            int i = 0;
            ByteIntConvert.UInt2Bytes(MessageLength).CopyTo(headerBytes, i);
            i += 4;
            ByteIntConvert.UInt2Bytes((uint)_CommandID).CopyTo(headerBytes, i);
            i += 4;
            SequenceNumber.ToByte().CopyTo(headerBytes, i);
            return headerBytes;
        }

        public override uint CommandHeaderLength
        {
            get { throw new NotImplementedException(); }
        }
    }
}
